package com.sun.push;

import com.baidu.yun.core.log.YunLogEvent;
import com.baidu.yun.core.log.YunLogHandler;
import com.baidu.yun.push.auth.PushKeyPair;
import com.baidu.yun.push.client.BaiduPushClient;
import com.baidu.yun.push.constants.BaiduPushConstants;
import com.baidu.yun.push.exception.PushClientException;
import com.baidu.yun.push.exception.PushServerException;
import com.baidu.yun.push.model.PushMsgToAllRequest;
import com.baidu.yun.push.model.PushMsgToAllResponse;
import com.baidu.yun.push.model.PushMsgToSingleDeviceRequest;
import com.baidu.yun.push.model.PushMsgToSingleDeviceResponse;


public class BaiduPushMessage {
	
	private BaiduPushClient pushClient = null;
	
	public static String CHANNEL_ID = "3860279117925175389";
	private final static String API_KEY = "KvQdRdrW0wckEISfua5HNyOH";
	private final static String SECRET_KEY = "BjPOM3G7nap95Xq2tiUulQ3Hok7liGCW";
	public final static String DEFAULT_MSG = "{\"title\":\"最新通知\",\"description\":\"LOL有新公告拉，请点击查看\"}";
	
	// 设备类型定义
	public final static Integer DEVICE_WEB = 1;
	public final static Integer DEVICE_PC = 2;
	public final static Integer DEVICE_ANDROID = 3;
	public final static Integer DEVICE_IOS = 4;
	public final static Integer DEVICE_WP = 5;
	
	// 传输类型定义
	public final static Integer MSG_TYPE_TOU_PUSH = 0;
	public final static Integer MSG_TYPE_NOTICE_PUSH = 1;
	
	public BaiduPushMessage(){
		init();
	}
	
	private void init() {
		pushClient = new BaiduPushClient(new PushKeyPair(API_KEY, SECRET_KEY),
	            BaiduPushConstants.CHANNEL_REST_URL);
		
		 // 注册YunLogHandler，获取本次请求的交互信息
	    pushClient.setChannelLogHandler (new YunLogHandler () {
	        @Override
	        public void onHandle (YunLogEvent event) {
	            System.out.println(event.getMessage());
	        }
	    });
	}
	
	public void pushMessageForChannel(String strChannelID, String strMsg, 
			Integer deviceType, Integer msgType, Integer vaildTime ){
		try {
	        	// 设置请求参数，创建请求实例
	            PushMsgToSingleDeviceRequest request = new PushMsgToSingleDeviceRequest().
	                addChannelId(strChannelID).
	                addMsgExpires(vaildTime).   //设置消息的有效时间,单位秒,默认3600*5.
	                addMessageType(msgType).             //设置消息类型,0表示透传消息,1表示通知,默认为0.
	                addMessage(strMsg).
	                addDeviceType(deviceType);      //设置设备类型，deviceType => 1 for web, 2 for pc, 
	                                       //3 for android, 4 for ios, 5 for wp.
	            // 执行Http请求
	            PushMsgToSingleDeviceResponse response = pushClient.
	                pushMsgToSingleDevice(request);
	            // Http请求返回值解析
	            System.out.println("msgId: " + response.getMsgId()
	                    + ",sendTime: " + response.getSendTime());
	        } catch (PushClientException e) {
	            //ERROROPTTYPE 用于设置异常的处理方式 -- 抛出异常和捕获异常,
	            //'true' 表示抛出, 'false' 表示捕获。
	        	e.printStackTrace();
	        } catch (PushServerException e) {
                System.out.println(String.format(
                        "requestId: %d, errorCode: %d, errorMsg: %s",
                        e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
	        }
	}
	
	public void pushMessageForAll(String strMsg, Integer deviceType, Integer msgType, Integer vaildTime){
		try {
        	// 设置请求参数，创建请求实例
			PushMsgToAllRequest request = new PushMsgToAllRequest().
                addMsgExpires(vaildTime).   //设置消息的有效时间,单位秒,默认3600*5.
                addMessageType(msgType).             //设置消息类型,0表示透传消息,1表示通知,默认为0.
                addMessage(strMsg).
                addDeviceType(deviceType);      //设置设备类型，deviceType => 1 for web, 2 for pc, 
                                       //3 for android, 4 for ios, 5 for wp.
            // 执行Http请求
			PushMsgToAllResponse response = pushClient.pushMsgToAll(request);
			
            // Http请求返回值解析
            System.out.println("msgId: " + response.getMsgId()
                    + ",sendTime: " + response.getSendTime());
        } catch (PushClientException e) {
            //ERROROPTTYPE 用于设置异常的处理方式 -- 抛出异常和捕获异常,
            //'true' 表示抛出, 'false' 表示捕获。
        	e.printStackTrace();
        } catch (PushServerException e) {
            System.out.println(String.format(
                    "requestId: %d, errorCode: %d, errorMsg: %s",
                    e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
        }
			
	}
	
}